package com.tqfframe.website.web.config;

import com.tqfframe.testStarter.domain.Hello;
import com.tqfframe.website.web.config.condition.LinuxCondition;
import com.tqfframe.website.web.config.condition.WindowsCondition;
import com.tqfframe.website.web.config.domain.*;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @Author: 唐祺峰
 * @Date: 2020/12/25 0025 16:35
 * @Version 1.0
 */
@Configuration
@ConditionalOnClass(name="com.tqfframe.website.web.config.domain.TestClient")     //只有我存在时，我才会被注入
public class TestConfig {

    @Bean(name = "hello")
    public Hello hello() {
        Hello hello=new Hello();
        hello.setMsg("我在自己项目注入了，就不使用自动注入配置了");
        return hello;
    }


    //创建这个bean的必要条件,matches方法必须为true才会注册bean，反之忽略bean。
    @Bean(name = "service")
    @Conditional(WindowsCondition.class)
    public ListService windowsService() {
        return new WindowsService();
    }

    //创建这个bean的必要条件,matches方法必须为true才会注册bean，反之忽略bean。
    @Bean(name = "service")
    @Conditional(LinuxCondition.class)
    public ListService linuxEmailerService() {
        return new LinuxService();
    }

    @Bean("notebookPC")
    public Computer computer1(){
        return new Computer("笔记本电脑");
    }
    //如果Computer已经被注入，我就不注入了
    @ConditionalOnMissingBean
    @Bean("reservePC")
    public Computer computer2(){
        return new Computer("备用电脑");
    }

    public void testSayHello(){
        System.out.println("你好呀");
    }

}
